package com.atguigu.controller;

import com.atguigu.common.R;
import com.atguigu.dto.SetmealDto;
import com.atguigu.entity.Category;
import com.atguigu.entity.Setmeal;
import com.atguigu.service.impl.CategoryServiceImpl;
import com.atguigu.service.impl.SetmealDishServiceImpl;
import com.atguigu.service.impl.SetmealServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Resource
    private SetmealServiceImpl setmealService;
    @Resource
    private SetmealDishServiceImpl setmealDishService;
    @Resource
    private CategoryServiceImpl categoryService;

    /**
     * 添加套餐
     *
     * @param
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto) {
        log.info(setmealDto.toString());
        setmealService.addSetmealWithDish(setmealDto);
        return R.success("套餐添加成功");
    }

    /**
     * 显示套餐列表
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page=" + page + ",pageSize=" + pageSize + ",name=" + name);
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        //新建SetmealDto的page
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //根据name获取套餐信息
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(StringUtils.isNotBlank(name), Setmeal::getName, name);

        //分页
        setmealService.page(pageInfo, setmealLambdaQueryWrapper);
        //将pageInfo中的setmeal转换成为setmealDto
        BeanUtils.copyProperties(pageInfo, setmealDtoPage, "records");
        List<Setmeal> records = pageInfo.getRecords();
        //新建一个集合作为setmealDtoPage的records
        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();


        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);
            //根据categoryId查找categoryName
            Category category = categoryService.getById(record.getCategoryId());
            //在SetmealDto中设置categoryName属性
            setmealDto.setCategoryName(category.getName());
            //放入到setmealDtos
            setmealDtos.add(setmealDto);
        }

        setmealDtoPage.setRecords(setmealDtos);
        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐及其相应的套餐-菜品关系数据
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info(ids.toString());
        setmealService.removeSetmealWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    /**
     * 查询套餐列表
     * @param setmeal
     * @return
     */
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        log.info(setmeal.toString());
        //根据categoryId和status查找相应的套餐
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        //根据更新时间进行排序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }


}

